package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.*;
import com.jy.adscm.wechat.service.ShMachineService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatRoleService;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-24
 */
@Service
public class ShMachineServiceImpl extends ServiceImpl<ShMachineMapper, ShMachine> implements ShMachineService {


    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatUserRoleMapper  wechatUserRoleMapper;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Override
    public Page<ShMachine> selectMachines(Page<ShMachine> page, QueryShMachine queryShMachine) {
        //查出这个用户信息
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryShMachine.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return page;
        }
        //查询这个是否工作人员还是浆站人员
        Wrapper<WechatUserRole> wechatUserRoleWrapper=new EntityWrapper<>();
        wechatUserRoleWrapper.eq("user_id",wechatUser.getId());
        List<WechatUserRole> wechatUserRoles = wechatUserRoleMapper.selectList(wechatUserRoleWrapper);
         Integer  pid=null;
        for (WechatUserRole wechatUserRole : wechatUserRoles) {
            if (wechatUserRole.getRoleId()==1){
                pid=wechatUser.getPlasmaStationId();
            }
        }

        Wrapper<ShMachine> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(pid,null)){
            wrapper.eq("pid",pid);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShMachine.getKeyWords(),null)){
            wrapper.andNew();
            wrapper.like("type",queryShMachine.getKeyWords());
            wrapper.or();
            wrapper.like("mac_code",queryShMachine.getKeyWords());
        }

        page=super.selectPage(page,wrapper);
        List<ShMachine> records = page.getRecords();
        for (ShMachine record : records) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(record.getPid(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(record.getPid());
                if (AdscmBaseUtils.getInstance().isNotEmpty(shCustomerInfo,null)){
                    record.setPlasmaStationName(shCustomerInfo.getName());
                }

            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public List<ShMachine> selectMachineLists(String openid) throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        Boolean jzUser = wechatUserRoleService.isJzUser(openid);
        Wrapper<ShMachine> wrapper=new EntityWrapper<>();
        if (jzUser){
            wrapper.eq("pid",wechatUser.getPlasmaStationId());
        }
        List<ShMachine> shMachines = super.selectList(wrapper);
        return shMachines;
    }

    @Override
    public ShMachine selectByMacCode(String macCode) {
 Wrapper<ShMachine> wrapper=new EntityWrapper<>();
 wrapper.eq("mac_code",macCode);
        List<ShMachine> shMachines = super.selectList(wrapper);
        ShMachine shMachine =new ShMachine();
        if (shMachines!=null&& shMachines.size()>0){
            shMachine=shMachines.get(0);
            if (AdscmBaseUtils.getInstance().isNotEmpty(shMachine.getPid(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(shMachine.getPid());
                shMachine.setPlasmaStationName(shCustomerInfo.getName());
            }
        }
        return shMachine;
    }
}
